function format(value, row, index) {
	var str = '<a  name="after-sale-server-details" href="#" class="easyui-linkbutton edit-btn"></a>'
			+ '<a  name="after-sale-server-delete" href="#" class="easyui-linkbutton edit-btn"></a>'
			+ '<a  name="after-sale-server-edit" href="#" class="easyui-linkbutton edit-btn"></a>';
	return str;

}

/* 清除数据表格中的数据 */
function clearDataGrid() {
	// 获取当前页的记录数
	var item = $('#jm-after-sale-server-datagrid').datagrid('getRows');
	if (item != null) {
		for (var i = item.length - 1; i >= 0; i--) {
			var index = $('#jm-after-sale-server-datagrid').datagrid('getRowIndex', item[i]);
			$('#jm-after-sale-server-datagrid').datagrid('deleteRow', index);
		}
	}
}

$(function() {
	$("#jm-after-sale-server-datagrid").datagrid({
		onLoadSuccess : function(data) {
			// datagrid的右侧的图片按钮
			// 视图
			$("a[name='after-sale-server-details']").linkbutton({
				width : 30,
				iconCls : 'icon-my_detail',
				plain : true,
				onClick : function() {
					$("#jm-after-sale-server-datagrid").datagrid({
						onClickCell : function(index) {
							var rows = $("#jm-after-sale-server-datagrid").datagrid('getRows');// 获得所有行
							var row = rows[index];// 根据index获得其中一行
							if (row) {
								$('#view-after-sale-server-form').form('clear');
								$('#view-after-sale-server-dialog').window('open').panel({
									onLoad : function() {
										$.ajax({
											url : 'find-contact-by-contactid',
											async : false,// 改为同步请求
											type : 'POST',
											data : {
												contactid : row.clientId
											},
											success : function(e) {
												$('#view-after-sale-server-form-client-combobox').combobox('setValue', e.clientInfo.cid);
											}
										});
										$('#view-after-sale-server-form-contact-combobox').combobox('setValue', row.clientId);

										$('#view-after-sale-server-theme').textbox('setValue', row.theme);

										$('#view-after-sale-server-form input[name=type][value=' + row.type + ']').attr("checked", "true");
										$('#view-after-sale-server-form input[name=way][value=' + row.way + ']').attr("checked", "true");
										$('#view-after-sale-server-startTime').datetimebox('setValue', row.startTime);
										$('#view-after-sale-server-spendTime').combobox('setValue', row.spendTime);
										$('#view-after-sale-server-form input[name=state][value=' + row.state + ']').attr("checked", "true");
										$('#view-after-sale-server-exertId').combobox('setValue', row.exertId);
										$('#view-after-sale-server-context').textbox('setValue', row.context);
										$('#view-after-sale-server-feedback').textbox('setValue', row.feedback);
										$('#view-after-sale-server-remarks').textbox('setValue', row.remarks);
									}
								});
							} else {
								$.messager.alert("提示", "请先选中要编辑的行")
							}
							$("#jm-after-sale-server-datagrid").datagrid({
								onClickCell : function(index) {
								}
							});
						}
					});
				}
			});
			// 删除
			$("a[name='after-sale-server-delete']").linkbutton({
				width : 30,
				iconCls : 'icon-my_delete',
				plain : true,
				onClick : function() {
					$("#jm-after-sale-server-datagrid").datagrid({
						onClickCell : function(index) {
							var rows = $('#jm-after-sale-server-datagrid').datagrid('getRows');// 获得所有行
							var row = rows[index];// 根据index获得其中一行。
							$.messager.confirm("询问", "亲，您确定要删除吗？", function(v) {
								if (v) {
									var serverId = row.serverId;
									$.post('del-after-sale-server', {
										serverId : serverId
									}, function(r) {
										if (r.success) {
											$("#jm-after-sale-server-datagrid").datagrid('reload');
										} else {
											$.messager.alert("提示", r.messager);
										}
									});
								}
							});
							$("#jm-after-sale-server-datagrid").datagrid({
								onClickCell : function(index) {
								}
							});
						}
					});
				}
			});
			// 编辑
			$("a[name='after-sale-server-edit']").linkbutton({
				width : 30,
				iconCls : 'icon-my_edit',
				plain : true,
				onClick : function() {
					$("#jm-after-sale-server-datagrid").datagrid({
						onClickCell : function(index) {
							var rows = $("#jm-after-sale-server-datagrid").datagrid('getRows');// 获得所有行
							var row = rows[index];// 根据index获得其中一行
							if (row) {
								$('#edit-after-sale-server-dialog').window('open').panel({
									onLoad : function() {
										$.ajax({
											url : 'find-contact-by-contactid',
											async : false,// 改为同步请求
											type : 'POST',
											data : {
												contactid : row.clientId
											},
											success : function(e) {
												$('#edit-after-sale-server-form-client-combobox').combobox('setValue', e.clientInfo.cid);
											}
										});
										$('#edit-after-sale-server-form-contact-combobox').combobox('setValue', row.clientId);
										$('#edit-after-sale-server-theme').textbox('setValue', row.theme);

										$('#edit-after-sale-server-form input[name=serverId]').val(row.serverId);
										$('#edit-after-sale-server-form input[name=type][value=' + row.type + ']').attr("checked", "true");
										$('#edit-after-sale-server-form input[name=way][value=' + row.way + ']').attr("checked", "true");
										$('#edit-after-sale-server-startTime').datetimebox('setValue', row.startTime);
										$('#edit-after-sale-server-spendTime').combobox('setValue', row.spendTime);
										$('#edit-after-sale-server-form input[name=state][value=' + row.state + ']').attr("checked", "true");
										$('#edit-after-sale-server-exertId').combobox('setValue', row.exertId);
										$('#edit-after-sale-server-context').textbox('setValue', row.context);
										$('#edit-after-sale-server-feedback').textbox('setValue', row.feedback);
										$('#edit-after-sale-server-remarks').textbox('setValue', row.remarks);
									}
								});
							} else {
								$.messager.alert("提示", "请先选中要编辑的行")
							}
							$("#jm-after-sale-server-datagrid").datagrid({
								onClickCell : function(index) {
								}
							});
						}
					});
				}
			});
		}
	});

	/* 点击头部搜索框中的搜索框进行搜所 */
	$("#after-sale-server-searchbox").searchbox({
		searcher : function() {
			// 获取快速搜所类型
			var searchname = $('#after-sale-server-searchbox').searchbox("getName");
			// 获取搜索框中的值
			var searchVal = $("#after-sale-server-searchbox").searchbox("getValue");

			if (searchname == 'serverId') {
				var pattern = /^[1-9]\d*$/;
				if (!pattern.test(searchVal)) {
					alert('亲，您输入的编号格式不正确');
					return;
				}
				clearDataGrid();
				$('#jm-after-sale-server-datagrid').datagrid({
					queryParams : null
				});
				$('#jm-after-sale-server-datagrid').datagrid({
					url : 'find-after-sale-server-by-serverId',
					method : 'post',
					queryParams : {
						serverId : searchVal
					},
				});
				$('#jm-after-sale-server-datagrid').datagrid('reload');
			} else if (searchname == "theme") {
				clearDataGrid();
				$('#jm-after-sale-server-datagrid').datagrid({
					queryParams : null
				});
				$('#jm-after-sale-server-datagrid').datagrid({
					url : 'find-after-sale-server-by-theme',
					method : 'post',
					queryParams : {
						theme : searchVal
					},
				});
				$('#jm-after-sale-server-datagrid').datagrid('reload');
			}
		}
	});

	// 新建
	$('#after-sale-server-tools-btn-add').linkbutton({
		onClick : function() {
			$('#add-after-sale-server-dialog').window('open');
		}
	});

	// 删除
	$('#after-sale-server-tools-btn-remove').linkbutton({
		onClick : function() {
			var rows = $("#jm-after-sale-server-datagrid").datagrid('getChecked');
			if (rows.length == 0) {
				$.messager.alert("提示", "亲，请先选择要删除的行！");
			} else {
				$.messager.confirm("询问", "亲，您确定要删除吗？", function(v) {
					if (v) {
						for (var i = 0; i < rows.length; i++) {
							if (rows[i]) {
								var serverId = rows[i].serverId;
								$.post('del-after-sale-server', {
									serverId : serverId
								}, function(r) {
									if (r.success) {
										$("#jm-after-sale-server-datagrid").datagrid('reload');
									} else {
										$.messager.alert("提示", r.messager);
									}
								});
							}
						}
					}
				});
			}
		}
	});

	// 查询所有
	$('#after-sale-server-tools-btn-selectAll').linkbutton({
		onClick : function() {
			$('#jm-after-sale-server-datagrid').datagrid({
				url : 'find-after-sale-server-by-page',
				method : 'post',
				queryParams : null
			});
		}
	});
})